import { _decorator, Component, Node, EventTouch, EventKeyboard, Vec2, input, Input, KeyCode, Animation, Vec3, director, game } from 'cc';
import { emits } from '../data/enums';
import { gameConfig } from '../data/gameConfig';
import { levelConfig } from '../data/levelConfig';
const { ccclass, property } = _decorator;

@ccclass('player')
export class player extends Component {
    private touchStartPos: Vec2 = new Vec2();
    private touchEndPos: Vec2 = new Vec2();
    private blockSize = gameConfig.blockSize;
    private keyIsOK = true;
    private gameWindowNode: Node = null;
    

    @property(Animation)
    anim: Animation = null; // 引用动画组件
    protected onEnable(): void {
        this.gameWindowNode = this.node.parent.parent;
        this.gameWindowNode.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.gameWindowNode.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);

        // 键盘事件监听
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        //注册键盘禁用
        director.on(emits.banKey, () => {
            this.keyIsOK = false;
        }, this)
        // 注册复用
        director.on(emits.enAbleKey, () => {
            this.keyIsOK = true;
        }, this)
    }

    onTouchStart(event: EventTouch) {
        const location: Vec2 = event.getLocation(); // 获取 Vec2 类型的位置
        this.touchStartPos.set(location.x, location.y); // 转换为 Vec3 并设置 z=0
    }

    onTouchEnd(event: EventTouch) {
        const location: Vec2 = event.getLocation(); // 获取 Vec2 类型的位置
        this.touchEndPos.set(location.x, location.y); // 转换为 Vec3 并设置 z=0
        this.handleTouchMovement();
    }

    handleTouchMovement() {
        const moveThreshold = 50; // 设置滑动阈值
        const deltaX = this.touchEndPos.x - this.touchStartPos.x;
        const deltaY = this.touchEndPos.y - this.touchStartPos.y;

        if (Math.abs(deltaX) > Math.abs(deltaY)) {
            // 水平滑动
            if (deltaX > moveThreshold) {
                // 向右滑动
                this.movePlayer(new Vec2(1, 0), 'walkRight');
            } else if (deltaX < -moveThreshold) {
                // 向左滑动
                this.movePlayer(new Vec2(-1, 0), 'walkLeft');
            }
        } else {
            // 垂直滑动
            if (deltaY > moveThreshold) {
                // 向上滑动
                this.movePlayer(new Vec2(0, 1), 'walkUp');
            } else if (deltaY < -moveThreshold) {
                // 向下滑动
                this.movePlayer(new Vec2(0, -1), 'walkDown');
            }
        }
    }

    onKeyDown(event: EventKeyboard) {
        const keyCode = event.keyCode;
        switch (keyCode) {
            case KeyCode.KEY_W:
                this.movePlayer(new Vec2(0, 1), 'walkUp');
                break;
            case KeyCode.KEY_A:
                this.movePlayer(new Vec2(-1, 0), 'walkLeft');
                break;
            case KeyCode.KEY_S:
                this.movePlayer(new Vec2(0, -1), 'walkDown');
                break;
            case KeyCode.KEY_D:
                this.movePlayer(new Vec2(1, 0), 'walkRight');
                break;
        }
    }

    movePlayer(direction: Vec2, animationName: string) {
        // 播放相应的动画
        if (this.anim) {
            this.anim.play(animationName);
        }
        let playerNextPos = [gameConfig.currentPos[0] + direction.x, gameConfig.currentPos[1] + direction.y];
        let tmp = gameConfig.currentLevel[playerNextPos[0]][playerNextPos[1]]
        if (this.keyIsOK) {
            if (tmp == -1 || tmp == 2) {
                gameConfig.currentPos = playerNextPos;
                // // 根据方向移动玩家
                director.emit(emits.playerMove);
                director.emit(emits.updateStep);   
            }else if(tmp == 1){
                let boxNextpos = [gameConfig.currentPos[0] + 2*direction.x, gameConfig.currentPos[1] + 2*direction.y];
                let boxTmp = gameConfig.currentLevel[boxNextpos[0]][boxNextpos[1]]
                if(boxTmp == -1|| boxTmp == 2){
                    //箱子移动然后人物移动
                    //先取得箱子结点
                    gameConfig.boxNodes.forEach(box => {
                        let boxTs:any = box.getComponent('box');
                        //证明是在推的这个箱子
                        if(boxTs.position[0]==playerNextPos[0]&&boxTs.position[1]==playerNextPos[1]){
                            gameConfig.currentLevel[boxNextpos[0]][boxNextpos[1]] = 1;
                            gameConfig.currentLevel[playerNextPos[0]][playerNextPos[1]] = -1;
                            boxTs.position = boxNextpos;
                            boxTs.boxSetPos();
                            //推完监测当前状态
                            director.emit(emits.gameCheck);
                        }
                    });
                }
            }else{
                console.log('不能移动');
            }
        }
    }
}